package com.darcye.clear.work;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import com.darcye.clear.reader.LayoutFileReader;
import com.darcye.clear.reader.OnFileReadListener;
import com.darcye.clear.reader.entity.BaseEntity;
import com.darcye.clear.reader.entity.LayoutEntity;
import com.darcye.clear.utils.DeleteLogger;
import com.darcye.clear.utils.FileList;

/**
 * 清理layout文件
 * @author Darcy
 * @date 2014-9-26 下午6:14:51
 * @version V1.0
 */
public class LayoutWorker extends BaseWorker{
	
	private DeleteLogger logger = new DeleteLogger("Layout");
	
	private Set<String> mScanLayoutIdSet = new LinkedHashSet<String>();
	
	private Set<String> mScannedLayoutIdSet = new HashSet<String>();
	
	public LayoutWorker(JavaWorker javaWorker,MenuWorker menuWorker){
		mUsedLayoutIdSet.addAll(javaWorker.getUsedLayoutIdSet());
		mUsedLayoutIdSet.addAll(menuWorker.getUsedLayoutIdSet());
		mScanLayoutIdSet.addAll(mUsedLayoutIdSet);
	}
	
	public void execute() {
		deepScanLayouts();
		deleteLayoutFiles();
		logger.finish();
	}
	
	/**
	 * 深度扫描layout资源
	 */
	private void deepScanLayouts(){
		for (String layoutId : mScanLayoutIdSet) {
			recurseScanLayout(layoutId);
		}
	}
	
	private void recurseScanLayout(String layoutId){
		if(mScannedLayoutIdSet.contains(layoutId))return;
		mScannedLayoutIdSet.add(layoutId);
		List<File> diffLayoutFiles = getDiffLayoutFiles(layoutId);
		for (File fFile : diffLayoutFiles) {
			LayoutFileReader layoutReader = new LayoutFileReader(fFile);
			try {
				layoutReader.setOnFileReadListener(new OnFileReadListener() {
					public void onReaded(BaseEntity entity) {
						LayoutEntity layoutEntity = (LayoutEntity)entity;
						mUsedLayoutIdSet.add(layoutEntity.getLayoutFile());
						mUsedStyleIdSet.addAll(layoutEntity.getStyleIdSet());
						mUsedAnimIdSet.addAll(layoutEntity.getAnimIdSet());
						mUsedAnimatorIdSet.addAll(layoutEntity.getAnimatorIdSet());
						mUsedTransitionIdSet.addAll(layoutEntity.getTransitionIdSet());
						mUsedDrawableIdSet.addAll(layoutEntity.getDrawableIdSet());
						mUsedColorIdSet.addAll(layoutEntity.getColorIdSet());
						mUsedStringIdSet.addAll(layoutEntity.getStringIdSet());
						mUsedIntegerIdSet.addAll(layoutEntity.getIntegerIdSet());
						mUsedBoolIdSet.addAll(layoutEntity.getBoolIdSet());
						mUsedDimenIdSet.addAll(layoutEntity.getDimenIdSet());
						mUsedIdIdSet.addAll(layoutEntity.getIdIdSet());
						for (String layoutId : layoutEntity.getLayoutIdSet()) {
							recurseScanLayout(layoutId);
						}
					}
				}).scan();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 删除的layout资源
	 */
	private void deleteLayoutFiles(){
		String[] diffLayoutPaths = getDiffLayoutPath();
		FileList fileList;
		List<File> layoutFiles;
		String fName;
		String layoutId;
		for (String layoutPath : diffLayoutPaths) {
			 fileList = new FileList(layoutPath);
			 layoutFiles =  fileList.recurseListFiles();
			 for(File layoutFile: layoutFiles){
				 if(isIgnoreDeleteFile(layoutFile))
					 continue;
				 fName = layoutFile.getName();
				 layoutId = fName.substring(0, fName.indexOf("."));
				 if(!mUsedLayoutIdSet.contains(layoutId)){
					 logger.log(layoutId, layoutFile.getPath());
					 backupDelFile(layoutFile);
					 layoutFile.delete();
				 }
			 }
		}
	}
	
}
